#include <iostream>
#include "lib/transport.hpp"
#include "boost/thread.hpp"
using namespace std;
using namespace kport;
using namespace pland;

void pusher(SharedDQueue q)
{
    while(true)
    {
        char* buff = new char[1024*1024];
        Data_Header *h = new Data_Header;
        h->size = 1024*1024;
        SharedData d(new Data(buff, h));
        q->push(d);
        boost::this_thread::sleep(boost::posix_time::microseconds(10000));
    }
}

void poper(SharedDQueue q)
{
    int count = 0;
    while(true)
    {
        SharedData d = q->pop();
        count++;
        if(count%10000 == 0)
            cout << "." << flush;
    }
}

int main()
{
    Transport t("route.xml");
    pland::Chain::Chain_Queue qin;
    qin.net = "In";
    qin.sources.push_back(1);
    qin.sinks.push_back(2);
    qin.queue = SharedDQueue(new DQueue());
    qin.type = pland::Chain::SOURCE;

    pland::Chain::Chain_Queue qout;
    qout.net = "Out";
    qout.sources.push_back(2);
    qout.sinks.push_back(3);
    qout.queue = SharedDQueue(new DQueue());
    qout.type = pland::Chain::SINK;

    std::vector<Chain::Chain_Queue> qs;
    qs.push_back(qin);
    qs.push_back(qout);

    t.loadQueues(qs);
    t.startListner();
    sleep(1);
    t.connectOutbound();
    sleep(1);
    t.start();
    boost::thread_group g;
    g.add_thread(new boost::thread(pusher, qout.queue));
    g.add_thread(new boost::thread(poper, qin.queue));
    char a;
    cin >> a;
    return 0;
}
